#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char const *argv[])
{
    // 1.打开读文件流
    int fd1 = open("love1.txt", O_RDONLY);
    if (fd1 == -1)
    {
        printf("打开读文件失败\n");
        return -1;
    }

    // 2.打开写文件流
    int fd2 = open("love2.txt", O_CREAT | O_WRONLY | O_APPEND);
    if (fd2 == -1)
    {
        printf("打开写文件失败\n");
        return -1;
    }

    // 3.边读边写
    char data_buff[100];
    int read_len;
    while ((read_len = read(fd1, data_buff, sizeof(data_buff))) > 0)
    {
        write(fd2, data_buff, read_len);
        // 4. 写到控制台上去
        write(STDOUT_FILENO, data_buff, read_len);
    }

    // 5.关闭文件流
    close(fd1);
    close(fd2);

    return 0;
}
